<?php

namespace App\Http\Controllers\Xcx;

use App\Logic\WxLogin;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use JWTAuth;
use Cache;
use App\Guest;

class AuthController extends Controller
{
    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\WxLoginException
     * @throws \GuzzleHttp\Exception\GuzzleException
     * @throws \Exception
     * @throws \Psr\SimpleCache\InvalidArgumentException
     */
    public function login(Request $request)
    {
        $user = (new WxLogin())->login($request->post('code'));
        if (!Cache::has('user_token_'.$user->id)){
            $token = JWTAuth::fromUser($user);
            Cache::set('user_token_'.$user->id,$token,Carbon::now()->addSeconds(7000));
        }else{
            $token = Cache::get('user_token_'.$user->id);
        }
        return $this->success(compact('token'));
    }
    public function test(){
        $user = Guest::findOrFail(82);
        $token = JWTAuth::fromUser($user);
        return $this->success(compact('token'));
    }

}
